package com.backend.room.service.impl;

import java.util.List;
import com.backend.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.backend.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.backend.room.domain.RoomType;
import com.backend.room.mapper.RoomMapper;
import com.backend.room.domain.Room;
import com.backend.room.service.IRoomService;

/**
 * 房间管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-10-04
 */
@Service
public class RoomServiceImpl implements IRoomService 
{
    @Autowired
    private RoomMapper roomMapper;

    /**
     * 查询房间管理
     * 
     * @param roomId 房间管理主键
     * @return 房间管理
     */
    @Override
    public Room selectRoomByRoomId(Long roomId)
    {
        return roomMapper.selectRoomByRoomId(roomId);
    }

    /**
     * 查询房间管理列表
     * 
     * @param room 房间管理
     * @return 房间管理
     */
    @Override
    public List<Room> selectRoomList(Room room)
    {
        return roomMapper.selectRoomList(room);
    }

    /**
     * 新增房间管理
     * 
     * @param room 房间管理
     * @return 结果
     */
    @Transactional
    @Override
    public int insertRoom(Room room)
    {
        room.setCreateTime(DateUtils.getNowDate());
        int rows = roomMapper.insertRoom(room);
        insertRoomType(room);
        return rows;
    }

    /**
     * 修改房间管理
     * 
     * @param room 房间管理
     * @return 结果
     */
    @Transactional
    @Override
    public int updateRoom(Room room)
    {
        room.setUpdateTime(DateUtils.getNowDate());
//        roomMapper.deleteRoomTypeByRoomId(room.getRoomId());
        insertRoomType(room);
        return roomMapper.updateRoom(room);
    }

    /**
     * 批量删除房间管理
     * 
     * @param roomIds 需要删除的房间管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteRoomByRoomIds(Long[] roomIds)
    {
        roomMapper.deleteRoomTypeByRoomIds(roomIds);
        return roomMapper.deleteRoomByRoomIds(roomIds);
    }

    /**
     * 删除房间管理信息
     * 
     * @param roomId 房间管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteRoomByRoomId(Long roomId)
    {
        roomMapper.deleteRoomTypeByRoomId(roomId);
        return roomMapper.deleteRoomByRoomId(roomId);
    }

    /**
     * 新增房间类型信息
     * 
     * @param room 房间管理对象
     */
    public void insertRoomType(Room room)
    {
        List<RoomType> roomTypeList = room.getRoomTypeList();
        Long roomId = room.getRoomId();
        if (StringUtils.isNotNull(roomTypeList))
        {
            List<RoomType> list = new ArrayList<RoomType>();
            for (RoomType roomType : roomTypeList)
            {
                if(roomType.getRoomTypeId() != null){
                    roomMapper.updateRoomType(roomType);
                }else{
                    roomType.setRoomId(roomId);
                    list.add(roomType);
                }
            }
            if (list.size() > 0)
            {
                roomMapper.batchRoomType(list);
            }
        }
    }
}
